package com.zmn.plat.admin.controller.channel.mark;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.model.entity.channel.mark.ChannelMark;
import com.zmn.plat.model.entity.channel.mark.ChannelMarkQuery;
import com.zmn.plat.services.interfaces.channel.mark.ChannelMarkGroupService;
import com.zmn.plat.services.interfaces.channel.mark.ChannelMarkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 渠道标示基础修改Controller
 *
 * @author jiafei
 * @since 2021/12/06 11:23
 */
@Slf4j
@RestController
@RequestMapping("/channel/mark/modify/")
public class ChannelMarkModifyController extends PlatBaseController {

    @Resource
    private ChannelMarkService channelMarkService;

    @Resource
    private ChannelMarkGroupService channelMarkGroupService;

    /**
     * 描述: 保存
     *
     * @author jiafei
     * @since 2021/12/06 11:23
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(@RequestBody ChannelMark channelMark) {

        //校验
        List<ChannelMark> channelMarks = channelMarkService.listByQuery(ChannelMarkQuery.builder().name(channelMark.getName()).build());
        if (CollectionUtil.isNotNullOrEmpty(channelMarks) && !Objects.equals(channelMark.getMarkId(), channelMarks.get(0).getMarkId())) {
            return ResponseDTO.fail("标示名称已存在");
        }

        Date now = DateUtil.getNow();
        Integer markId = channelMark.getMarkId();
        try {
            LoginStaff mcStaff = getMcStaff();
            String operator = mcStaff.getDeptName() + "-" + mcStaff.getRealName();
            channelMark.setUpdater(operator);
            channelMark.setUpdateTime(now);
            if (NumberUtil.isNullOrZero(markId)) {
                channelMark.setStatus(GlobalConsts.YES);
                channelMark.setCreater(operator);
                channelMark.setCreateTime(now);
                channelMarkService.insert(channelMark);
            } else {
                channelMarkService.updateByKey(channelMark);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 修改状态
     * @param ids    id集合
     * @param status 状态
     *
     * @author jiafei
     * @since 2021/12/06 11:23
     */
    @RequestMapping(value = "/updateStatus")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "ids") String ids,
                                             @RequestParam(value = "status") Integer status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            channelMarkService.updateStatusBatch(arrayIds, status, getMcStaff());
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
            return ResponseDTO.fail("修改状态失败");
        }
    }

    /**
     * 功能描述: 修改排序
     *
     * @param markId 主键
     * @param sort 排序
     * @author jiafei
     * @since 2021/12/06 11:23
     */
    @RequestMapping(value = "/updateSort")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "id") Integer markId,
                                             @RequestParam(value = "sort") Integer sort) {

        if (NumberUtil.isNullOrZero(markId)) {
            return ResponseDTO.fail("请选择要更改排序的数据");
        }
        if (NumberUtil.isNullOrZero(sort)) {
            return ResponseDTO.fail("请设置排序值");
        }
        try {
            ChannelMark channelMark = ChannelMark.builder()
                    .markId(markId)
                    .sort(sort)
                    .updater(getMcStaff().getDeptName() + "-" + getMcStaffRealName())
                    .updateTime(DateUtil.getNow())
                    .build();
            channelMarkService.updateByKey(channelMark);
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            return ResponseDTO.fail("修改排序失败");
        }
    }

    /**
     * 描述: 删除
     * @param markId 主键
     *
     * @author jiafei
     * @since 2021/12/06 11:23
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "markId") Integer markId) {

        try {
            if (NumberUtil.isNullOrZero(markId)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            channelMarkService.deleteByKey(markId);
            return ResponseDTO.success("删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }


    /**
     * 描述: 修改状态
     *
     * @param addIds id集合
     * @param markId 标识Id
     * @param delIds 原关注Id
     * @author jiafei
     * @since 2021/12/06 11:23
     */
    @RequestMapping(value = "/group/assChannels/update")
    public ResponseDTO<String> updateAssChannels(@RequestParam(value = "addIds", required = false) String addIds,
                                                 @RequestParam(value = "markId") Integer markId,
                                                 @RequestParam(value = "delIds", required = false) String delIds) {

        if (StringUtil.isBlank(addIds) && StringUtil.isBlank(delIds)) {
            return ResponseDTO.fail("请选择要更改关联的渠道！");
        }
        List<Integer> assIds = null;
        if (StringUtil.isNotBlank(addIds)) {
            assIds = StringUtil.convertStrsToList(addIds);
        }
        List<Integer> cancelIds = null;
        if (StringUtil.isNotBlank(delIds)) {
            cancelIds = StringUtil.convertStrsToList(delIds);
        }

        try {
            channelMarkGroupService.modifyAssChannels(assIds, markId, cancelIds, getMcStaff());
            return ResponseDTO.success(null, "操作成功！");
        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
            return ResponseDTO.fail("修改状态失败");
        }
    }

    /**
     * 描述: 修改渠道关联的标识
     *
     * @param oldMarkId  旧标识ID
     * @param newMarkId  新标识ID
     * @param channelIds 渠道ID
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.String>
     * @author jiafei
     * @since 2021/12/9 10:19
     */
    @RequestMapping(value = "/group/updateChannelAssMark")
    public ResponseDTO<String> updateChannelAssMark(@RequestParam(value = "oldMarkId") Integer oldMarkId,
                                                    @RequestParam(value = "newMarkId") Integer newMarkId,
                                                    @RequestParam(value = "channelIds") String channelIds) {

        if (NumberUtil.isNullOrZero(oldMarkId) && NumberUtil.isNullOrZero(newMarkId)) {
            return ResponseDTO.fail("请选择要更改关联的标识！");
        }
        List<Integer> channelList = StringUtil.convertStrsToList(channelIds);
        if (CollectionUtil.isNullOrEmpty(channelList)) {
            return ResponseDTO.fail("请选择要更改关联的渠道！");
        }
        channelMarkGroupService.updateChannelAssMark(oldMarkId, newMarkId, channelList, getMcStaff());
        return ResponseDTO.success();
    }

}
